package com.feeyin.radar.msg.dao.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.model.Contract;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.SaleRecord;
import com.feeyin.radar.msg.dao.InternalMailboxDAO;
import com.feeyin.radar.msg.dao.WarningInfoDAO;
import com.feeyin.radar.msg.model.WarningInfo;
import com.feeyin.radar.sys.dao.DataInterfaceDAO;
import com.feeyin.radar.sys.dao.ManageSetDAO;
import com.feeyin.radar.sys.model.ManageSet;
import java.util.Date;
import java.util.List;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class WarningInfoDAOImpl extends SqlMapClientDaoSupport
  implements WarningInfoDAO
{
  private ManageSetDAO manageSetDAO;
  private CustomerDAO customerDAO;
  private InternalMailboxDAO internalMailboxDAO;
  private DataInterfaceDAO dataInterfaceDAO;

  public ManageSetDAO getManageSetDAO()
  {
    return this.manageSetDAO;
  }

  public void setManageSetDAO(ManageSetDAO manageSetDAO) {
    this.manageSetDAO = manageSetDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public InternalMailboxDAO getInternalMailboxDAO()
  {
    return this.internalMailboxDAO;
  }

  public void setInternalMailboxDAO(InternalMailboxDAO internalMailboxDAO) {
    this.internalMailboxDAO = internalMailboxDAO;
  }

  public DataInterfaceDAO getDataInterfaceDAO() {
    return this.dataInterfaceDAO;
  }

  public void setDataInterfaceDAO(DataInterfaceDAO dataInterfaceDAO) {
    this.dataInterfaceDAO = dataInterfaceDAO;
  }

  public List getPageWarningInfosByDynamic(WarningInfo warningInfo, Page page) {
    if (warningInfo == null) return null;
    if (page == null) return null;
    warningInfo.setPageStartRow(page.getPageStartRow());
    warningInfo.setPageEndRow(page.getPageEndRow());
    return getSqlMapClientTemplate().queryForList("getPageWarningInfosByDynamic", warningInfo);
  }

  public Integer getPageWarningInfosCountByDynamic(WarningInfo warningInfo) {
    if (warningInfo == null) return null;
    return (Integer)getSqlMapClientTemplate().queryForObject("getPageWarningInfosCountByDynamic", warningInfo);
  }

  public List getPageWarningInfosByUserId(Integer userId, Page page) {
    if (userId == null) return null;
    if (page == null) return null;
    WarningInfo warningInfo = new WarningInfo();
    warningInfo.setWarningUserId(userId);
    warningInfo.setPageStartRow(page.getPageStartRow());
    warningInfo.setPageEndRow(page.getPageEndRow());
    return getSqlMapClientTemplate().queryForList("getPageWarningInfosByDynamic", warningInfo);
  }

  public Integer getPageWarningInfosCountByUserId(Integer userId) {
    if (userId == null) return null;
    WarningInfo warningInfo = new WarningInfo();
    warningInfo.setWarningUserId(userId);
    return (Integer)getSqlMapClientTemplate().queryForObject("getPageWarningInfosCountByDynamic", warningInfo);
  }

  public boolean insertWarningInfo(WarningInfo warningInfo) {
    if (warningInfo == null) return false;
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertWarningInfo", warningInfo);
    return result != null;
  }

  public boolean insertAuditWarningInfoForContract(Contract contract)
  {
    if (contract == null) return false;
    if (!this.manageSetDAO.isStartWarning()) return false;

    ManageSet manageSet = new ManageSet();
    manageSet = this.manageSetDAO.getManageSetByCode("auditRemind");
    if (manageSet == null) return false;
    if ((StringUtils.isEmpty(manageSet.getStatus())) || (!"1".equals(manageSet.getStatus()))) return false;
    String userIds = StringUtils.getSubLastComma(manageSet.getMValue());
    if (StringUtils.isEmpty(userIds)) return false;

    Customer customer = new Customer();
    customer = this.customerDAO.getCustomerById(contract.getCustomerId());
    if (customer == null) return false;

    WarningInfo warningInfo = new WarningInfo();
    warningInfo.setForeignCode(contract.getContractCode());
    warningInfo.setForeignId(contract.getId());
    warningInfo.setForeignName(customer.getCustomerName());
    warningInfo.setIsRead("1");
    warningInfo.setSponsorId(contract.getSalePersonId());
    warningInfo.setWarningCode("auditRemind");
    warningInfo.setWarningDate(new Date());
    warningInfo.setWarningSource("1");
    warningInfo.setUserIds(userIds);
    insertWarningInfo(warningInfo);

    this.internalMailboxDAO.insertInternalMail(warningInfo, 4);

    this.dataInterfaceDAO.insertEMail(warningInfo, 4);
    return true;
  }

  public boolean insertAuditWarningInfoForSaleCost(String saleCostCode, Integer applyPersonId) {
    if (StringUtils.isEmpty(saleCostCode)) return false;
    if (applyPersonId == null) return false;
    if (!this.manageSetDAO.isStartWarning()) return false;

    ManageSet manageSet = new ManageSet();
    manageSet = this.manageSetDAO.getManageSetByCode("auditRemind");
    if (manageSet == null) return false;
    if ((StringUtils.isEmpty(manageSet.getStatus())) || (!"1".equals(manageSet.getStatus()))) return false;
    String userIds = StringUtils.getSubLastComma(manageSet.getMValue());
    if (StringUtils.isEmpty(userIds)) return false;

    WarningInfo warningInfo = new WarningInfo();
    warningInfo.setForeignCode(saleCostCode);
    warningInfo.setIsRead("1");
    warningInfo.setSponsorId(applyPersonId);
    warningInfo.setWarningCode("auditRemind");
    warningInfo.setWarningDate(new Date());
    warningInfo.setWarningSource("2");
    warningInfo.setUserIds(userIds);
    insertWarningInfo(warningInfo);

    this.internalMailboxDAO.insertInternalMail(warningInfo, 5);

    this.dataInterfaceDAO.insertEMail(warningInfo, 5);
    return true;
  }

  public boolean insertMoveCustomerWarningInfo(Customer customer) {
    if (!this.manageSetDAO.isStartWarning()) return false;
    if (customer == null) return false;
    if (customer.getSalePersonId() == null) return false;

    ManageSet manageSet = new ManageSet();
    manageSet = this.manageSetDAO.getManageSetByCode("openSeaRemind");
    if (manageSet == null) return false;
    if ((StringUtils.isEmpty(manageSet.getStatus())) || (!"1".equals(manageSet.getStatus()))) return false;
    String userIds = StringUtils.getSubLastComma(manageSet.getMValue());
    if (StringUtils.isEmpty(userIds)) return false;
    if (userIds.indexOf(customer.getSalePersonId().toString()) <= -1) return false;

    WarningInfo warningInfo = new WarningInfo();
    warningInfo.setForeignCode(customer.getCustomerCode());
    warningInfo.setForeignId(customer.getId());
    warningInfo.setForeignName(customer.getCustomerName());
    warningInfo.setIsRead("1");

    warningInfo.setWarningUserId(customer.getSalePersonId());
    warningInfo.setWarningCode("openSeaRemind");
    warningInfo.setWarningDate(new Date());
    warningInfo.setWarningSource("3");
    warningInfo.setIsAging("2");
    insertWarningInfo(warningInfo);

    this.internalMailboxDAO.insertInternalMail(warningInfo, 2);

    this.dataInterfaceDAO.insertEMail(warningInfo, 2);
    return true;
  }

  public boolean insertWarningOverDueCustomer(Customer customer, long agingDay) {
    if (!this.manageSetDAO.isStartWarning()) return false;
    if (customer == null) return false;
    if (customer.getSalePersonId() == null) return false;
    if (agingDay <= 0L) return false;

    ManageSet manageSet = new ManageSet();
    manageSet = this.manageSetDAO.getManageSetByCode("openSeaRemind");
    if (manageSet == null) return false;
    if ((StringUtils.isEmpty(manageSet.getStatus())) || (!"1".equals(manageSet.getStatus()))) return false;
    String userIds = StringUtils.getSubLastComma(manageSet.getMValue());
    if (StringUtils.isEmpty(userIds)) return false;
    if (userIds.indexOf(customer.getSalePersonId().toString()) <= -1) return false;

    WarningInfo warningInfo = new WarningInfo();
    warningInfo.setForeignCode(customer.getCustomerCode());
    warningInfo.setForeignId(customer.getId());
    warningInfo.setForeignName(customer.getCustomerName());
    warningInfo.setIsRead("1");

    warningInfo.setWarningUserId(customer.getSalePersonId());
    warningInfo.setWarningCode("openSeaRemind");
    warningInfo.setWarningDate(new Date());
    warningInfo.setWarningSource("3");
    warningInfo.setIsAging("1");
    warningInfo.setAgingDay(Integer.valueOf(Integer.parseInt(agingDay+"")));
    insertWarningInfo(warningInfo);

    this.internalMailboxDAO.insertInternalMail(warningInfo, 3);

    this.dataInterfaceDAO.insertEMail(warningInfo, 3);
    return true;
  }

  public boolean insertCurrentScheduleWarningInfo(SaleRecord saleRecord)
  {
    if ((saleRecord == null) || (saleRecord.getCustomerId() == null) || 
      (saleRecord.getContactDate() == null)) return false;

    if (!this.manageSetDAO.isStartWarning()) return false;
    boolean result = false;

    ManageSet manageSet = new ManageSet();
    manageSet = this.manageSetDAO.getManageSetByCode("scheduleRemind");
    if (manageSet == null) return false;
    if ((StringUtils.isEmpty(manageSet.getStatus())) || (!"1".equals(manageSet.getStatus()))) return false;
    String userIds = StringUtils.getSubLastComma(manageSet.getMValue());
    if (StringUtils.isEmpty(userIds)) return false;

    Date curDate = DateUtils.getDateByFormat(new Date(), "yyyy-MM-dd");
    if (curDate.compareTo(saleRecord.getContactDate()) == 0) {
      Customer customer = new Customer();
      customer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());
      if (customer != null) {
        WarningInfo warningInfo = new WarningInfo();
        warningInfo.setForeignId(saleRecord.getId());
        warningInfo.setForeignName(customer.getCustomerName());
        warningInfo.setIsAging("1");
        warningInfo.setIsRead("1");
        warningInfo.setWarningCode("scheduleRemind");
        warningInfo.setWarningDate(new Date());
        warningInfo.setWarningSource("4");
        warningInfo.setWarningUserId(saleRecord.getCreatePersonId());
        warningInfo.setOtherId(saleRecord.getContactWayId());
        if ((curDate.compareTo(saleRecord.getContactDate()) == 0) && 
          (!StringUtils.isEmpty(saleRecord.getContactTime())))
          warningInfo.setAgingDay(Integer.valueOf(Integer.parseInt(saleRecord.getContactTime())));
        else if ((curDate.compareTo(saleRecord.getNextContactDate()) == 0) && 
          (!StringUtils.isEmpty(saleRecord.getNextContactTime()))) {
          warningInfo.setAgingDay(Integer.valueOf(Integer.parseInt(saleRecord.getNextContactTime())));
        }
        insertWarningInfo(warningInfo);

        this.internalMailboxDAO.insertInternalMail(warningInfo, 0);

        this.dataInterfaceDAO.insertEMail(warningInfo, 0);
        result = true;
      }
    }
    return result;
  }

  public boolean updateWarningInfoForProcessDate(Integer foreignId, String warningSource, String foreignCode)
  {
    if (StringUtils.isEmpty(warningSource)) return false;

    WarningInfo warningInfo = new WarningInfo();
    if (StringUtils.isEmpty(foreignCode)) {
      if (foreignId == null) return false;
      warningInfo.setForeignId(foreignId);
    } else {
      warningInfo.setForeignCode(foreignCode);
    }
    warningInfo.setWarningSource(warningSource);
    warningInfo.setIsRead("2");
    warningInfo.setProcessDate(new Date());

    return updateWarningInfoByDynamic(warningInfo);
  }

  public boolean updateWarningInfoByDynamic(WarningInfo warningInfo) {
    if (warningInfo == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateWarningInfoByDynamic", warningInfo));
    return result != null;
  }

  public boolean deleteWarningInfo(Integer id)
  {
    if (id == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().delete("deleteWarningInfo", id));
    return result != null;
  }
}